package model;

import state.FarmMachineContext;
import state.IdleState;
import state.MaintenanceState;
import state.WorkingState;

public class Tractor implements FarmMachine {
    private final String deviceId;
    private boolean isRunning = false;
    private String currentParams;

    public void setRunning(boolean running) {
        isRunning = running;
    }

    private FarmMachineContext stateContext;

    //jia
public String getCurrentParams() {
    // 这里应该返回当前实际的参数值
    // 假设我们有一个字段存储当前参数
    return this.currentParams;
}
    public void adjust(String params) {
        System.out.println("Tractor " + deviceId + " 调整参数: " + params);
        // 实际调整参数逻辑
        this.currentParams = params;
    }

    
    
    
    public Tractor(String deviceId) {
        this.deviceId = deviceId;
        this.stateContext = new FarmMachineContext();
    }



    // 实现 AgriculturalDevice 接口
    @Override
    public void start() {
        if (stateContext.getState() instanceof IdleState) {
            if (!isRunning) {
                isRunning = true;
                System.out.println("Tractor " + deviceId + " 启动");
                stateContext.request(); // 触发状态转换
            }
        } else {
            System.out.println("Tractor " + deviceId + " 不在待机状态，无法启动");
        }
    }

    @Override
    public void stop() {
    if (isRunning) {
        isRunning = false;
        System.out.println("Tractor " + deviceId + " 停止");
        stateContext.setState(new IdleState());
    }}

    @Override
    public void emergencyStop() {
        isRunning = false;
        System.out.println("Tractor " + deviceId + " 紧急停止");
    }


    @Override
    public String getDeviceId() {
        return deviceId;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    // 实现 FarmMachine 接口
    @Override
    public void performTask() {
        if (isRunning) {
            System.out.println("Tractor " + deviceId + " 执行耕地任务");
        }
    }

    // 实现 EnvironmentObserver 接口（若有）
    @Override
    public void update(String sensorType, double value) {
        System.out.println("Tractor " + deviceId + " 接收传感器数据: " + sensorType + "=" + value);
    }
    @Override
    public String getName() {
        return "拖拉机";
    }

    @Override
    public String getType() {
        return "TRACTOR";
    }

    @Override
    public String getStatus() {
        return isRunning() ? "运行中" : "停止";
    }
    @Override
    public String getStatusDescription() {
        if (stateContext.getState() instanceof IdleState) {
            return "待机";
        } else if (stateContext.getState() instanceof WorkingState) {
            return "运行中";
        } else if (stateContext.getState() instanceof MaintenanceState) {
            return "维护中";
        }
        return "未知状态";
    }

    @Override
    public FarmMachineContext getStateContext() {
        return stateContext;
    }

    @Override
    public void setStateContext(FarmMachineContext stateContext) {
        this.stateContext = stateContext;
    }
}